package com.cskt.util;
/*
* json序列化工具*/

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

public class JsonUtil {
    private static ObjectMapper objectMapper;
    static {
        objectMapper = new ObjectMapper();
    }
    /**
     * 对象转换为Json字符串
     * @param object
     * @return
     * @throws JsonProcessingException
     */
    public static String objectToJsonString(Object object) throws JsonProcessingException {

        String s = objectMapper.writeValueAsString(object);
        return s;
    }

    /**
     * 将json字符串反序列化为Object对象
     * @param JsonString 待反序列化的json字符串
     * @param tClass 返回的类类型
     * @param <T>泛型
     * @return
     * @throws IOException
     */
    public static<T> T jsonStringToObject(String JsonString,Class<T> tClass) throws IOException {

        return  objectMapper.readValue(JsonString,tClass);

    }
}
